﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Data;
using ClassLibrary.Entity;
using System.Data;

namespace ClassLibrary.DAO
{
    public class TblMenuDAO
    {
        private DBService _db = null;

        public TblMenuDAO(String connectionString)
        {
            _db = new DBService(connectionString);
        }

        public bool InSert_Up(TblMenu menu, int mode)
        {
            bool flag = false;
            try
            {
                int result = 0;
                result = _db.ExeSP_NonQuery("sp_TblMenu_InUp",
                    new String[] { "@IDMenu", "@MenuName", "@IDType", "@ListMenu", "@Price",  "@Mode" },
                    new object[] { menu.IDMenu, menu.MenuName, menu.IDType, menu.ListMenu, menu.Price, mode}, CommandType.StoredProcedure);

                flag = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        public DataTable GetMenu_Wedding(int idtype)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("Select * from vMenu where IDType = @idtype ", 
                                        new string[]{"@IDType"},
                                        new object[]{idtype} , CommandType.Text);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        //public TblMenu GetMenuByID(int id)
        //{
        //    TblMenu menu = new TblMenu();
        //    try
        //    {
        //        DataTable dt = _db.ExeSP_DataTable("select *, TypeName from TblMenu, TblTpe where IDMenu = @id and TblMenu.IDTpe = TblType.IDType",
        //            new string[] {"@id" },
        //            new object[] {id }, CommandType.Text);
        //        foreach (DataRow dr in dt.Rows)
        //        {
        //            menu.IDMenu = id;
        //            menu.IDType = Convert.ToInt32(dr["IDType"].ToString());
        //            menu.ListMenu = dr["ListMenu"].ToString();
        //            menu.MenuName = dr["MenuName"].ToString();
        //            menu.Price = Decimal.Parse(dr["Price"].ToString());
                    
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //    return menu;
        //}
        public DataTable GetMenuByID(int id)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select vMenu.* from vMenu where IDMenu  = @id",
                    new string[] {"@id" },
                    new object[] {id }, CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;

        }
        
         public TblMenu GetMenu_YumCha_ByID(int IDMenu)
         {
             TblMenu menu = new TblMenu();
             try
             {
                 DataTable dt = _db.ExeSP_DataTable("select vMenu.* from vMenu where IDType = 1 and IDMenu = @id",
                     new string[] { "@id" },
                     new object[] { IDMenu }
                     , CommandType.Text);
                 foreach (DataRow dr in dt.Rows)
                 {
                     menu.IDMenu = IDMenu;
                  
                     menu.MenuName = dr["MenuName"].ToString();
                     menu.ListMenu = dr["ListMenu"].ToString();                  
                     menu.IDType = Convert.ToInt32(dr["IDType"].ToString());
                     menu.Price = Decimal.Parse(dr["Price"].ToString());
                     menu.ImageURL = dr["ImageURL"].ToString();


                    
                 }
             }
             catch (Exception ex)
             {
                 throw ex;
             }
             return menu;
         }


        public bool DeleteImgMenu(TblImageMenu imgMenu)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("delete TblMenuImage where ID =@id",
                    new string[] { "@id" },
                    new object[] { imgMenu.ID },
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        public bool InsImageMenu(TblImageMenu imgMenu, int mode)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("sp_TblImageMenu_Ins",
                    new string[] { "@p_IDMenu", "@p_ImageURL" },
                    new object[] { imgMenu.IDMenu, imgMenu.ImageURL },
                    CommandType.StoredProcedure);
                flag = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        public DataTable GetImageMenu(int IDMenu)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select * from TblMenuImage where IDMenu = @IDMenu",
                    new string[] { "@IDMenu" },
                    new object[] { IDMenu },
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
    }
}
